﻿using IMDemo.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace IMDemo.Views
{
    /// <summary>
    /// UCGroupList.xaml 的交互逻辑
    /// </summary>
    public partial class UCGroupList : UserControl
    {
        private ObservableCollection<UCGroup> groupList;

        public ObservableCollection<UCGroup> GroupList
        {
            get
            {
                if (this.groupList == null)
                {
                    this.groupList = new ObservableCollection<UCGroup>();
                }

                return this.groupList;
            }
        }

        public UCGroup SelectedGroup
        {
            get { return (UCGroup)GetValue(SelectedGroupProperty); }
            set { SetValue(SelectedGroupProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedGroup.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedGroupProperty =
            DependencyProperty.Register("SelectedGroup", typeof(UCGroup), typeof(UCGroupList), new PropertyMetadata(null));

        public UCGroupList()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public void LoadGroupList(agsXMPP.XmppClientConnection conn, List<ContactModel> list)
        {
            foreach (var item in list)
            {
                UCGroup ucGroup = new Views.UCGroup(conn, item);
                ucGroup.SendMessage += UcGroup_SendMessage;
                ucGroup.ReceivedMessage += UcGroup_ReceivedMessage;
                this.GroupList.Add(ucGroup);
            }
        }

        private void UcGroup_ReceivedMessage(object sender, MessageModel message)
        {
            this.OnReceivedMessage((UCGroup)sender, message);
        }

        private void UcGroup_SendMessage(object sender, ContactModel to)
        {
            this.OnSendMessage(to);
        }

        #region 自定义事件
        #region SendMessage
        public delegate void SendMessageHandle(object sender, ContactModel to);

        public event SendMessageHandle SendMessage;

        public void OnSendMessage(ContactModel to)
        {
            if (this.SendMessage != null)
            {
                this.SendMessage(this, to);
            }
        }
        #endregion

        #region ReceivedMessage
        public delegate void ReceivedMessageHandle(object sender, UCGroup group, MessageModel message);

        public event ReceivedMessageHandle ReceivedMessage;

        public void OnReceivedMessage(UCGroup group, MessageModel message)
        {
            if (this.ReceivedMessage != null)
            {
                this.ReceivedMessage(this, group, message);
            }
        }
        #endregion
        #endregion
    }
}
